
layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        commonJS = layui.commonJS,
        validate = layui.validate,
        $view = $('#febs-accidentModuleConfig'),
        $query = $view.find('#query'),
        $searchForm = $view.find('#accidentModuleConfig-table-form'),
        $tenantSelect = $searchForm.find('select[name="tenantSelect"]'),
        $surveyReviewTypeDiv = $view.find('#surveyReviewTypeDiv'),
        $rectificationReviewTypeDiv = $view.find('#rectificationReviewTypeDiv'),
        tableIns;

    form.verify(validate);

    initFormValue();

    function initFormValue() {
        if (accidentModuleConfig) {
            let surveyReviewSwitch = accidentModuleConfig.surveyReviewSwitch
            let rectificationReviewSwitch = accidentModuleConfig.rectificationReviewSwitch
            form.val("accidentModuleConfig-form", {
                "moduleConfigId": accidentModuleConfig.moduleConfigId,
                "tenantId": accidentModuleConfig.tenantId,
                "surveyReviewSwitch": surveyReviewSwitch == 1 ? 'on' : '',
                "surveyReviewType": accidentModuleConfig.surveyReviewType,
                "rectificationReviewSwitch": rectificationReviewSwitch == 1 ? 'on' : '',
                "rectificationReviewType": accidentModuleConfig.rectificationReviewType,
            });
            if (surveyReviewSwitch == 1) {
                $surveyReviewTypeDiv.show();
            }else {
                $surveyReviewTypeDiv.hide();
            }
            if (rectificationReviewSwitch == 1) {
                $rectificationReviewTypeDiv.show();
            }else {
                $rectificationReviewTypeDiv.hide();
            }

        }
    }



    function reloadPage() {
        var tenantId=$tenantSelect.val();
        febs.get(ctx + 'accidentModuleConfig/search/' + tenantId, null, function (data) {
            var newAccidentModuleConfig = "";
            if(data.code==200&&data.data.accidentModuleConfig){
                newAccidentModuleConfig = data.data.accidentModuleConfig;
            }
            let surveyReviewSwitch = newAccidentModuleConfig.surveyReviewSwitch
            let rectificationReviewSwitch = newAccidentModuleConfig.rectificationReviewSwitch
            form.val("accidentModuleConfig-form", {
                "moduleConfigId": newAccidentModuleConfig.moduleConfigId,
                "tenantId": newAccidentModuleConfig.tenantId,
                "surveyReviewSwitch": surveyReviewSwitch == 1 ? 'on' : '',
                "surveyReviewType": newAccidentModuleConfig.surveyReviewType ? newAccidentModuleConfig.surveyReviewType : '1',
                "rectificationReviewSwitch": rectificationReviewSwitch == 1 ? 'on' : '',
                "rectificationReviewType": newAccidentModuleConfig.rectificationReviewType ? newAccidentModuleConfig.rectificationReviewType : '1',
            });
            if (surveyReviewSwitch == 1) {
                $surveyReviewTypeDiv.show();
            }else {
                $surveyReviewTypeDiv.hide();
            }
            if (rectificationReviewSwitch == 1) {
                $rectificationReviewTypeDiv.show();
            }else {
                $rectificationReviewTypeDiv.hide();
            }

        })
    }

    $query.on('click', function () {
        reloadPage();
    });

    // 监听调查审核开关
    form.on('switch(surveyReviewSwitch)', function (data) {
        let status = data.elem.checked;
        if (status) {
            $surveyReviewTypeDiv.show();
        }else {
            $surveyReviewTypeDiv.hide();
        }
    })
    // 监听整改审核开关
    form.on('switch(rectificationReviewSwitch)', function (data) {
        let status = data.elem.checked;
        if (status) {
            $rectificationReviewTypeDiv.show();
        }else {
            $rectificationReviewTypeDiv.hide();
        }
    })

    form.on('submit(accidentModuleConfig-form-submit)', function (data) {
        if(data.field.tenantId==null || data.field.tenantId ==''){
            data.field.tenantId = $tenantSelect.val();
        }
        data.field.surveyReviewSwitch = data.field.surveyReviewSwitch == 'on' ? 1 : 0
        data.field.rectificationReviewSwitch = data.field.rectificationReviewSwitch == 'on' ? 1 : 0
        febs.modal.confirm('保存提示', '是否保存配置信息？', function () {
            if (data.field.moduleConfigId && data.field.moduleConfigId.length > 0) {
                febs.postArray(ctx + 'accidentModuleConfig/update', data.field, function () {
                    layer.closeAll();
                    febs.alert.success('保存成功');
                });
            } else {
                febs.postArray(ctx + 'accidentModuleConfig', data.field, function () {
                    layer.closeAll();
                    febs.alert.success('保存成功');
                    reloadPage();
                });
            }
        });

        return false;
    });


    form.render();


});